#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>

using namespace std;

class Solution {
public:
    int removeDuplicates_1(vector<int> &nums)
    {
        sort(nums.begin(), nums.end()); // 排序
        auto end_unique = unique(nums.begin(), nums.end()); // 将不重复的排序到前面
        nums.erase(end_unique, nums.end()); // 删除重复的元素
        return nums.size(); // 返回删除后的长度
    }

    int removeDuplicates_2(vector<int> &nums)
    {
        int n = nums.size();
        if (n == 0) {
            return 0;
        }
        int fast = 1, slow = 1;
        while (fast < n) {
            if (nums[fast] != nums[fast - 1]) {
                nums[slow] = nums[fast];
                ++slow;
            }
            ++fast;
        }

        return slow;
    }
};

int main()
{
    vector<int> nums = {0, 1, 1, 3, 4, 5};
    vector<int> expectedNums;
    Solution s;
    int k = s.removeDuplicates_2(nums);
    for_each(nums.begin(), nums.end(),
             [&expectedNums](int i){ expectedNums.push_back(i);});
    assert(k == expectedNums.size());
    for (int i = 0; i < k; ++i) {
        assert(nums[i] == expectedNums[i]);
        cout << "ok" << endl;
    }

    return 0;
}
